package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Question;
import com.tanhua.dubbo.server.pojo.Soul;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class SoulApiImpl implements SoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Soul> findByUserId(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return mongoTemplate.find(query, Soul.class);
    }

    @Override
    public void saveSoul(Long userId, String name, String cover, String level, Integer star, Integer type, Integer isLock) {
        Soul soul = new Soul();
        soul.setId(ObjectId.get());
        soul.setUserId(userId);
        soul.setName(name);
        soul.setCover(cover);
        soul.setLevel(level);
        soul.setStar(star);
        Query query = Query.query(Criteria.where("type").is(type));
        List<Question> questions = mongoTemplate.find(query, Question.class);
        List<String> list = new ArrayList<>();
        for (Question question : questions) {
            list.add(question.getId());
        }
        soul.setQuestionIds(list);
        soul.setIsLock(isLock);
//        soul.setReportId();

        mongoTemplate.save(soul);
    }
}
